package com.whnk.workflow.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.whnk.common.support.Convert;
import com.whnk.common.utils.DataUtils;
import com.whnk.common.utils.DateUtils;
import com.whnk.framework.util.ShiroUtils;
import com.whnk.system.domain.SysUser;
import com.whnk.system.mapper.SequenceMapper;
import com.whnk.system.mapper.SysUserMapper;
import com.whnk.workflow.domain.Leave;
import com.whnk.workflow.domain.LeaveApr;
import com.whnk.workflow.mapper.LeaveAprMapper;
import com.whnk.workflow.mapper.LeaveMapper;
import com.whnk.workflow.service.ILeaveService;
import com.whnk.workflow.utils.WorkflowUtils;

/**
 * 请假 服务层实现
 * 
 * @author whwhnk
 * @date 2019-04-08
 */
@Service
public class LeaveServiceImpl implements ILeaveService
{
	@Autowired
	private LeaveMapper leaveMapper;
	@Autowired
	private LeaveAprMapper leaveAprMapper;
	@Autowired
	private SequenceMapper sequenceMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	
	/**
     * 查询请假信息
     * 
     * @param businessKey 工作流关联id
     * @return 请假信息
     */
    @Override
	public Leave selectLeaveById(String businessKey)
	{
	    return leaveMapper.selectLeaveById(businessKey);
	}
	
	/**
     * 查询请假列表
     * 
     * @param leave 请假信息
     * @return 请假集合
     */
	@Override
	public List<Leave> selectLeaveList(Leave leave)
	{
	    return leaveMapper.selectLeaveList(leave);
	}
	
    /**
     * 新增请假
     * 
     * @param leave 请假信息
     * @return 结果
     */
	@Override
	@Transactional
	public int insertLeave(Leave leave)
	{
		String leaveSeq = sequenceMapper.getSeqNextVal("leaveSeq");//请假序列号
		leaveSeq = DataUtils.seqFormat(leaveSeq, "0", 4);//格式化后序列号
		String businessKey = "leave" + DateUtils.dateTimeNow() + leaveSeq;//请假工作流关联id
		SysUser sysUser = ShiroUtils.getSysUser();//当前登陆用户
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("applicant", sysUser.getLoginName());//给下一节点分配任务处理人
		WorkflowUtils.startProcessInstance("leave", businessKey, map);//流程启动
		Task task = WorkflowUtils.findTaskByBusinessKey(businessKey);//根据businessKey查询当前任务，完成第一节点
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("branchManager", "yiran");//给下一节点分配任务处理人
		WorkflowUtils.complete(task.getId(), map2);//完成提交申请节点任务
		
		task = WorkflowUtils.findTaskByBusinessKey(businessKey);//根据businessKey查询当前任务
		SysUser su = sysUserMapper.selectUserByLoginName(task.getAssignee());//获取下一节点审批人user
		
		leave.setBusinessKey(businessKey);
		leave.setLoginName(sysUser.getLoginName());
		leave.setUserName(sysUser.getUserName());
		leave.setStep(task.getName());
		leave.setAprLoginName(su.getLoginName());
		leave.setAprUserName(su.getUserName());
		leave.setCreateBy(sysUser.getUserId().toString());
		leaveMapper.insertLeave(leave);
		
		LeaveApr leaveApr = new LeaveApr();
		leaveApr.setId(leaveAprMapper.selectNextId(businessKey));
		leaveApr.setBusinessKey(businessKey);
		leaveApr.setStep("提交申请");
		leaveApr.setStatus("1");
		leaveApr.setReason(leave.getReason());
		leaveApr.setAprLoginName(sysUser.getLoginName());
		leaveApr.setAprUserName(sysUser.getUserName());
		return leaveAprMapper.insertLeaveApr(leaveApr);
	}
	
	/**
     * 修改请假
     * 
     * @param leave 请假信息
     * @return 结果
     */
	@Override
	public int updateLeave(Leave leave)
	{
	    return leaveMapper.updateLeave(leave);
	}
	
	/**
     * 删除请假对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteLeaveByIds(String ids)
	{
		return leaveMapper.deleteLeaveByIds(Convert.toStrArray(ids));
	}

	/**
	 * 查询请假列表（未归档）
     * 
     * @param leave 请假信息
     * @return 请假集合
     */
	@Override
	public List<Leave> selectLeaveListUnfinished(Leave leave) {
		return leaveMapper.selectLeaveListUnfinished(leave);
	}
}
